package com.xz.knowledgeDuplication.pattern.framework.beans.context.support;

import com.xz.knowledgeDuplication.pattern.framework.beans.BeanDefinition;
import com.xz.knowledgeDuplication.pattern.framework.beans.context.ApplicationContext;
import com.xz.knowledgeDuplication.pattern.framework.beans.factory.support.BeanDefinitionReader;
import com.xz.knowledgeDuplication.pattern.framework.beans.factory.support.BeanDefinitionRegistry;

import java.util.HashMap;
import java.util.Map;

/**
 * ApplicationContext子实现类
 *
 * @author 召
 * @date 2022-04-11 21:50:06
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    /**
     * 解释器对象
     */
    protected BeanDefinitionReader beanDefinitionReader;

    /**
     * 用来存放bean对象的容器
     */
    protected Map<String, Object> singletonObjects = new HashMap<String, Object>();

    protected String configLocation;

    @Override
    public void refresh() throws Exception {
        // 加载BeanDefinition
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        // 初始化bean
        finishBeanInitialization();
    }

    /**
     * 初始化bean
     */
    private void finishBeanInitialization() throws Exception {
        // 获取注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        // 获取所有beanName
        String[] beanNames = registry.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            getBean(beanName);
        }
    }
}
